//
// NamedEvent_UNIX.h
//
// $Id: //poco/1.4/Foundation/include/Poco/NamedSemaphore_UNIX.h#1 $
//
// Library: Foundation
// Package: Processes
// Module:  NamedSemaphore
//
// Definition of the NamedSemaphoreImpl class for Unix.
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//


#ifndef Foundation_NamedSemaphore_UNIX_INCLUDED
#define Foundation_NamedSemaphore_UNIX_INCLUDED


#include "Poco/Foundation.h"
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) || LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
#include <semaphore.h>
#endif


namespace Poco {

enum SEMAPHORE_SHARE_TYPE {
	SEMAPHORE_SHARE_THREAD=0,
	SEMAPHORE_SHARE_PROCESS=1,
};

class Foundation_API NamedSemaphoreImpl
{
protected:
	// shareType only used when name is empty, equal to an unnamed semaphore
	NamedSemaphoreImpl(const std::string& name,SEMAPHORE_SHARE_TYPE shareType = SEMAPHORE_SHARE_THREAD);
	~NamedSemaphoreImpl();
	void setImpl();
	void waitImpl();
	bool tryWaitImpl();
		/// true: success, false fail
	
	bool tryWaitImpl(long milliseconds); 
		/// true: success, false fail
private:
	std::string getFileName();

	std::string m_name;
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) || LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
	sem_t* 	m_sem;
	sem_t 	m_unamedSem;
#else
	int m_lockfd; // lock file descriptor
	int m_semfd;  // file used to identify semaphore
	int m_semid;  // semaphore id
#endif
};


} // namespace Poco


#endif // Foundation_NamedEvent_UNIX_INCLUDED
